package com.situ.company.department.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.situ.company.department.dao.IDepartmentDao;
import com.situ.company.department.model.DepartmentModel;
import com.situ.company.util.JDBCUtil;

public class DepartmentDao2Impl implements IDepartmentDao{

	String table = "department";
	String cols  = "code,name,tel";
	@Override
	public Integer insert(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("insert into");
		sql.append(table).append("(").append(cols).append(")").append("values(?,?,?)");	
//		List<Object> values= Arrays.asList(model.getCode(),model.getName(),model.getTel());
//		return JDBCUtil.update(sql.toString(), values);
		return JDBCUtil.update(sql.toString(), model.getCode(),model.getName(),model.getTel());
	}

	@Override
	public Integer deleteByCode(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("delete from");
		sql.append(table).append("where").append("code").append("=").append("?");
//		List<Object> values= Arrays.asList(model.getCode());
//		return JDBCUtil.update(sql.toString(), values);
		return JDBCUtil.update(sql.toString(), model.getCode());
	}

	@Override
	public Integer delete(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("delete from").append(table); 
		List<Object> values= appendWhere(sql,model);
		return JDBCUtil.update(sql.toString(), values);
	} 

	@Override
	public Integer updateAll(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("update");
		sql.append(table).append("set name=?,tel=? where code=?");
		return JDBCUtil.update(sql.toString(), model.getName(),model.getTel(),model.getCode());
	}

	@Override
	public Integer updateActive(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("update");
		sql.append(table).append("set id=id,");
		List<Object> values= appendSet(sql,model);
		return JDBCUtil.update(sql.toString(), values);
	}

	@Override
	public List<DepartmentModel> selectList(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("select id,");
		sql.append(cols).append("from").append(table);
		List<Object> values = appendWhere(sql,model);
		return JDBCUtil.queryList(sql.toString(), values, DepartmentModel.class);
	}

	@Override
	public DepartmentModel selectModel(DepartmentModel model) {
		StringBuffer sql = new StringBuffer("select id,");
		sql.append(cols).append("from").append(table).append("where code=?") ;
		List<Object> values =Arrays.asList(model.getCode());
		return JDBCUtil.queryModel(sql.toString(), values, DepartmentModel.class);
	}
	
	private List<Object> appendWhere(StringBuffer sql, DepartmentModel model) {
		sql.append("where 1=1 ");
		List<Object> values = new ArrayList<>();
		String code = model.getCode();
		if(code!=null&&!code.isEmpty()) {
			sql.append("and code like ?");
			values.add(code);
		}
		String name = model.getName();
		if(name!=null&&!name.isEmpty()) {
			sql.append("and name like ?");
			values.add(name);
		}
		String tel = model.getTel();
		if(tel!=null&&!tel.isEmpty()) {
			sql.append("and tel like ?");
			values.add(tel);
		}
			
		return values;
	}
	
	private List<Object> appendSet(StringBuffer sql,DepartmentModel model){
		List<Object> values = new ArrayList<>();
		if(model.getName()!=null) {
			sql.append("name=?");
		values.add(model.getName());
		}
		if(model.getTel()!=null) {
			sql.append("tel=?");
		values.add(model.getTel());
		}
		sql.append("where code=?");
		values.add(model.getCode());
		return values;
	}

	@Override
	public Integer selectCount(DepartmentModel model) {
		// TODO Auto-generated method stub
		return null;
	}

}
